/*
 * Anything: A flexible and easy to use data structure that combines the 
 * characteristics of a list and a hashmap into one data structure.
 * 
 * Based on a paper by Peter Sommerlad and Marcel Rueedi on 
 * 'Do-it-yourself reflection' entered at EuroPLOP 1998.
 * 
 * This library was written completely from scratch and is based only 
 * in spirit on the original Java Anything implementation written by 
 * Andre Weinand/Marcel Rueedi in 1997/99 and enhanced by Stefan Tramm 
 * in 2006.
 * 
 * Copyright (c) 2008 Thomas Marti and others. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package any.functor.interfaces;

import static any.functor.Functions.asBigDecimal;
import static any.functor.Functions.asBool;
import static any.functor.Functions.asDouble;
import static any.functor.Functions.asNumber;
import static any.functor.Functions.asSerializable;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Map.Entry;

import any.ImmutableAny;
import any.functor.impl.pred.BaseFunctionTypedPredicate;
import any.functor.impl.pred.BasePredicateWithDescription;

public class NativePredicate extends BasePredicateWithDescription<Entry<String, ? extends ImmutableAny>> {

	public NativePredicate() {
		super("isNative");
	}
	
	public boolean apply(Entry<String, ? extends ImmutableAny> entry) {
		ImmutableAny any = entry.getValue();
		if (any.getType() == String.class) {
			return any.toNative().equals(any.toObject());
		} else {
			return true;					
		}
	}

	public PrimitiveFunctionTypedPredicate<Boolean, boolean[]> Bool() {
		return new BaseFunctionTypedPredicate<Boolean, boolean[]>("isNativeBool", asBool()) {
			public boolean apply(Entry<String, ? extends ImmutableAny> entry) {
				return (entry.getValue().toNative() instanceof Boolean);
			}
		};
	}
	
	public FunctionTypedPredicate<Number> Number() {
		return new BaseFunctionTypedPredicate<Number, Void>("isNativeNumber", asNumber()) {
			public boolean apply(Entry<String, ? extends ImmutableAny> entry) {
				return (entry.getValue().toNative() instanceof Number);
			}
		};
	}
	
	public FunctionTypedPredicate<Double> SpecialNumber() {
		return new BaseFunctionTypedPredicate<Double, double[]>("isNativeSpecialNumber", asDouble()) {
			public boolean apply(Entry<String, ? extends ImmutableAny> entry) {
				return (entry.getValue().toNative() instanceof Double);
			}
		};
	}
	
	public FunctionTypedPredicate<BigDecimal> RegularNumber() {
		return new BaseFunctionTypedPredicate<BigDecimal, Void>("isNativeRegularNumber", asBigDecimal()) {
			public boolean apply(Entry<String, ? extends ImmutableAny> entry) {
				return (entry.getValue().toNative() instanceof BigDecimal);
			}
		};
	}
	
	public FunctionTypedPredicate<Serializable> Serializable() {
		return new BaseFunctionTypedPredicate<Serializable, Void>("isNativeSerializable", asSerializable()) {
			public boolean apply(Entry<String, ? extends ImmutableAny> entry) {
				return (entry.getValue().toNative() instanceof Serializable);
			}
		};
	}
	
}
